package com.cml.infrastructure;


import com.cml.common.dto.entity.Message;
import com.cml.domain.service.ClientManagerService;
import com.cml.domain.entity.Session;
import org.apache.log4j.Logger;

/**
 * 姓名：陈茂林
 * 时间：2023/10/18 13:32
 * 描述：
 */
public class ClientMessageListener extends  Thread{

    Logger logger = Logger.getLogger(ClientMessageListener.class);
    private Session session;
    private ClientManagerService clientManagerService;


    private boolean isStop = false;
    public ClientMessageListener(Session session, ClientManagerService clientManagerService){
        this.session = session;
        this.clientManagerService = clientManagerService;
    }
    public void setStop(boolean stop) {
        isStop = stop;
    }
    @Override
    public void run() {
        while (!this.isStop){
            try {
                Message message = ClientQueueManage.takeUploadMessageMessage(session.getSessionId());
                Message reponseMessage = clientManagerService.process(session,message);
                if(reponseMessage != null){
                    ClientQueueManage.pushSendMessageMessage(session.getSessionId(),reponseMessage);
                }
            } catch (InterruptedException e) {
                logger.error(e);
            }
        }
    }
}
